package com.zhenyi.youqu.miniapp;

import java.util.ArrayList;
import java.util.List;

import com.cjw.model.DBMap;
import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.ehcache.CacheInterceptor;
import com.jfinal.plugin.ehcache.CacheName;
import com.youqu.common.model.YqApp;
import com.youqu.common.model.YqCategory;
import com.youqu.common.model.YqGroup;
import com.youqu.common.model.YqItem;
import com.youqu.common.model.YqProduct;
import com.youqu.common.model.YqProductFormat;
import com.youqu.common.model.YqProductGroup;
import com.youqu.common.model.YqUser;
import com.youqu.common.model.YqUserSubscribeProduct;
import com.zhenyi.youqu.core.YQMiniAppController;

/**
 * 商品
 * 
 * @author cen
 *
 */
public class ProductController extends YQMiniAppController {

	/**
	 * 快递费
	 * 
	 * @Time 2019年3月27日 下午10:31:48
	 */
	public void expressInfo() {
		YqApp app = getApp();
		DBMap map = new DBMap();
		map.put("expressCost", app.getExpressCost());
		map.put("expressFeeLimit", app.getExpressFeeLimit());
		map.put("expressLimit", app.getExpressLimit());
		renderSuccess(map);
	}

	/**
	 * 获取小程序商城页数据
	 */
//	@Before(CacheInterceptor.class)
//	@CacheName("1hour")
	public void initStore() {
		YqApp app = getApp();
		DBMap data = new DBMap();
		Long shopId = getParaShopId();
		YqItem banner = YqItem.dao.searchFirst("isTop", 1);
		if (banner != null && banner.getIsHidden().equals(0)) {
			data.put("banner", banner);
		}

		ArrayList<DBMap> lists = new ArrayList<>();
		DBMap guessCategory = new DBMap();
		Kv kv = getSimpleKv("shopId");
		kv.set("appId", app.getId());
		Integer isHot = getParaToInt("isHot", 0);
		if (isHot.equals(1)) {
			kv.set("isHot", 1);
		}
		Integer hotCount = getParaToInt("hotCount", 10);
		SqlPara guessSP = db.getSqlPara("YQFindProducts", kv);
		List<Record> guessProducts = db.paginate(1, hotCount, guessSP).getList();
		if (guessProducts.size() > 0) {
			guessCategory.put("title", "猜你喜欢");
			guessCategory.put("list", guessProducts);
			lists.add(guessCategory);
		}

		SqlPara categorySP = db.getSqlPara("YQCategories", Kv.by("appId", app.getId()).set("shopId", shopId));
		List<YqCategory> categories = YqCategory.dao.find(categorySP);
		for (YqCategory category : categories) {
			Kv categoryKv = Kv.by("categoryId", category.getId());
			if (shopId != null) {
				categoryKv.set("shopId", shopId);
			}
			SqlPara sp = db.getSqlPara("YQFindProducts", categoryKv);
			List<Record> products = db.find(sp);

			DBMap categoryMap = new DBMap();
			categoryMap.put("title", category.getTitle());
			categoryMap.put("list", products);
			if (category.getIsTop().equals(1)) {
				data.put("topList", categoryMap);
			} else {
				lists.add(categoryMap);
			}
		}
		data.put("lists", lists);
		// String key = "YQ_INIT_STORE";
		renderSuccess(data);
	}

	/**
	 * 商城banner
	 */
	public void banner() {
		YqItem banner = YqItem.dao.searchFirst("isTop", 1);
		renderSuccess(banner);
	}

	/**
	 * 商品列表
	 */
	// @Before(CacheInterceptor.class)
	// @CacheName("1hour")
	@Override
	public void list() {
		YqApp app = getApp();
		YqUser user = getUser();
		Long categoryId = getParaToLong("categoryId");
		Kv kv = Kv.create();
		kv.set("appId", app.getId());
		int type = getParaToInt("type", 1);
		kv.set("type", type);
		if (user != null) {
			kv.set("userId", user.getId());
		}
		if (categoryId != null) {
			kv.set("categoryId", categoryId);
		}
		SqlPara sqlPara = db.getSqlPara("YQFindProducts", kv);
		List<Record> items = db.paginate(getPage(), 10, sqlPara).getList();
		for (Record record : items) {
			if (record.getLong("userId") != null) {
				record.set("isSubscribe", 1);
			}
		}
		renderSuccess(items);
	}

	/**
	 * 热门商品
	 */
//	@Before(CacheInterceptor.class)
//	@CacheName("1hour")
	public void hotProducts() {
		YqApp app = getApp();
		Kv kv = getSimpleKv("categoryId");
		kv.set("isHot", 1);
		kv.set("appId", app.getId());
		SqlPara sqlPara = db.getSqlPara("YQFindProducts", kv);
		List<Record> items = db.paginate(getPage(), 10, sqlPara).getList();
		renderSuccess(items);
	}

	/**
	 * 最新商品
	 */
//	@Before(CacheInterceptor.class)
//	@CacheName("1hour")
	public void newProducts() {
		YqApp app = getApp();
		Kv kv = getSimpleKv("categoryId");
		kv.set("isNew", 1);
		kv.set("appId", app.getId());
		SqlPara sqlPara = db.getSqlPara("YQFindProducts", kv);
		List<Record> items = db.paginate(getPage(), 10, sqlPara).getList();
		renderSuccess(items);
	}

	/**
	 * 商品详情
	 */
	@Override
	public void info() {
		YqUser user = getUser();
		Long productId = getParaToLong("productId");
		if (productId == null) {
			renderError(404);
			return;
		}
		YqProduct product = YqProduct.dao.findById(productId);
		if (product == null) {
			renderError(404);
			return;
		}

		String shareTitle = product.getShareTitle();
		String shareImage = product.getShareImage();
		if (StrKit.notBlank(shareTitle)) {
			product.setShareTitle(shareTitle);
		}
		if (StrKit.notBlank(shareImage)) {
			product.setShareImage(shareImage);
		}
		Integer isGroup = product.getIsGroup();
		product.put("isGroup", isGroup);
		if (isGroup.equals(1)) {
			List<YqProductGroup> grpups = YqProductGroup.dao.findAll();
			product.put("grpups", grpups);
		} else {
			product.put("grpups", new ArrayList<>());
		}
		product.put("productId", productId);
		List<YqProductFormat> formats = product.getFormats(false);
		if (formats != null) {
			for (YqProductFormat format : formats) {
				format.set("fee", format.getInt("fee").floatValue() / 100);
			}
			product.put("formats", formats);
		}

		if (isGroup.equals(1)) {
			Kv kv = Kv.by("productId", productId);
			SqlPara sqlPara = db.getSqlPara("YQFindProductPaidUser", kv);
			List<Record> records = db.paginate(1, 10, sqlPara).getList();
			product.put("users", records);

			if (user != null) {
				YqGroup group = YqGroup.dao.findJoinedGroup(user.getId(), productId);
				if (group != null) {
					product.put("groupId", group.getId());
					product.put("group", group);
				}
			}
		}

		// 用户是否关注
		if (user != null) {
			YqUserSubscribeProduct subscribe = YqUserSubscribeProduct.dao.findById(productId, user.getId());
			product.put("isSubscribed", subscribe == null ? 0 : 1);
		}

		// product.put("product", product);
		DBMap map = new DBMap();
		map.put("product", product);
		renderSuccess(map);
	}

	public void recommend() {
		YqApp app = getApp();
		Kv kv = Kv.create();
		kv.set("isHot", 1);
		kv.set("appId", app.getId());
		SqlPara sqlPara = db.getSqlPara("YQFindProducts", kv);
		List<Record> items = db.paginate(getPage(), 10, sqlPara).getList();
		if (items.size() == 0) {
			LogKit.warn("no recommend");
			items = db.paginate(getPage(), 10, db.getSqlPara("YQFindProducts")).getList();
		}
		renderSuccess(items);
	}

	/**
	 * 餐饮优惠
	 */
	public void discount() {
		YqApp app = getApp();
		Kv kv = Kv.create();
		kv.set("type", 2);
		kv.set("appId", app.getId());
		SqlPara sqlPara = db.getSqlPara("YQFindProducts", kv);
		List<Record> items = db.paginate(getPage(), 10, sqlPara).getList();
		renderSuccess(items);
	}

	/**
	 * 活动门票
	 */
	public void ticket() {
		YqApp app = getApp();
		Kv kv = Kv.create();
		kv.set("type", 3);
		kv.set("appId", app.getId());
		SqlPara sqlPara = db.getSqlPara("YQFindProducts", kv);
		List<Record> items = db.paginate(getPage(), 10, sqlPara).getList();
		renderSuccess(items);
	}

}
